﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace BestHTTP
{
    /// <summary>
    /// Delegates some U3D calls to the HTTPManager.
    /// </summary>
    sealed class HTTPUpdateDelegator : MonoBehaviour
    {
        private static HTTPUpdateDelegator instance;
        public static void CheckInstance()
        {
            if (!instance)
            {
                instance = UnityEngine.Object.FindObjectOfType(typeof(HTTPUpdateDelegator)) as HTTPUpdateDelegator;

                if (!instance)
                {
                    GameObject go = new GameObject("HTTP Update Delegator");
                    go.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy;
                    UnityEngine.Object.DontDestroyOnLoad(go);

                    instance = go.AddComponent<HTTPUpdateDelegator>();
                }
            }
        }

        void Awake()
        {
            Caching.HTTPCacheService.SetupCacheFolder();
        }

        void LateUpdate()
        {
            HTTPManager.OnUpdate();
        }

#if UNITY_EDITOR
        void OnDisable()
        {
            HTTPManager.OnQuit();
        }
#else
        void OnApplicationQuit()
        {
            HTTPManager.OnQuit();
        }
#endif
    }
}